/**
*
*    simpleTooltip jQuery plugin, by Marius ILIE
*    visit http://dev.mariusilie.net for details
*
**/
(function($){ $.fn.simpletooltip = function(){
    return this.each(function() {
        var text = $(this).attr("title");
        $(this).attr("title", "");
        if(text != undefined) {
            $(this).hover(function(e){
                var tipX = e.pageX + 12;
                var tipY = e.pageY + 12;
                $(this).attr("title", "");
                $("body").append("<div id='simpleTooltip' style='position: absolute; z-index: 100; display: none;'>" + text + "</div>");
                if($.browser.msie) var tipWidth = $("#simpleTooltip").outerWidth(true)
                else var tipWidth = $("#simpleTooltip").width()
                $("#simpleTooltip").width(tipWidth);
                $("#simpleTooltip").css("left", tipX).css("top", tipY).fadeIn("medium");
            }, function(){
                $("#simpleTooltip").remove();
                $(this).attr("title", text);
            });
            $(this).mousemove(function(e){
                var tipX = e.pageX + 12;
                var tipY = e.pageY + 12;
                var tipWidth = $("#simpleTooltip").outerWidth(true);
                var tipHeight = $("#simpleTooltip").outerHeight(true);
                if(tipX + tipWidth > $(window).scrollLeft() + $(window).width()) tipX = e.pageX - tipWidth;
                if($(window).height()+$(window).scrollTop() < tipY + tipHeight) tipY = e.pageY - tipHeight;
                $("#simpleTooltip").css("left", tipX).css("top", tipY).fadeIn("medium");
            });
        }
    });
}})(jQuery);

$(document).ready(function() {
  $('.tooltip').simpletooltip();
});

function check_all(div, state)
{
  if(state)
    $("#" + div + " INPUT[type='checkbox']").attr('checked', true);
  else
    $("#" + div + " INPUT[type='checkbox']").attr('checked', false);
}

function popup(mylink, windowname, wwidth, hheight){
  if (! window.focus)return true;
  var href;

  if (typeof(mylink) != '')
     href=mylink;
  else
    href=mylink.href;

  var screenW = 640, screenH = 480;
  if (parseInt(navigator.appVersion)>3)
  {
     screenW = screen.width;
     screenH = screen.height;
  }
  else if (navigator.appName == "Netscape" && parseInt(navigator.appVersion)==3 && navigator.javaEnabled())
  {
     var jToolkit = java.awt.Toolkit.getDefaultToolkit();
     var jScreenSize = jToolkit.getScreenSize();
     screenW = jScreenSize.width;
     screenH = jScreenSize.height;
  }

  if (wwidth == '')
    wwidth = screenW;
  if (hheight == '')
     hheight = screenH - 60;

  window.open(href, windowname, "width=" + wwidth + ",height=" + hheight +",scrollbars=yes");

  return false;
}

/* PORUKE */
function primaociTop()
{
   $('#primaoci_top').html('');

   var primaoci_main = $('#primaoci_main').val();
   var primaoci_skola = $('#primaoci_skola').val();
   var primaoci_razred = $('#primaoci_razred').val();
   var controler = $('#controler').val();
   if(!primaoci_skola) primaoci_skola = 0;
   if(primaoci_skola == 'n') primaoci_skola = 0;
   if(!primaoci_razred) primaoci_razred = 0;
   if(primaoci_main == '1') primaoci_skola = 0;

   $.ajax({
      type: "POST",
      url: "/" + controler + "_dev.php/poruka/primaociTop",
      data: "tip=" + primaoci_main + "&skola_id=" + primaoci_skola + "&razred_id=" + primaoci_razred,
      success: function(msg){
        if(msg)
        {
           $('#primaoci_top').html(msg);
        }
      }
    });
}

function razredMenu()
{
   var primaoci_main = $('#primaoci_main').val();
   var primaoci_skola = $('#primaoci_skola').val();
   var controler = $('#controler').val();
   if(!primaoci_skola) primaoci_skola = 0;
   if(primaoci_main == '1') primaoci_skola = 0;
   if(primaoci_skola == 'n') return;

   $.ajax({
      type: "POST",
      url: "/" + controler + "_dev.php/poruka/razredMenu",
      data: "tip=" + primaoci_main + "&skola_id=" + primaoci_skola,
      success: function(msg){
        if(msg)
        {
           $('#razred_menu').html(msg);
        }
      }
    });
}

function resetRazred()
{
   $('#primaoci_razred').html('');
}

function send_poruka()
{
    $("#dugme").addClass("hide");
    var vrednost = '';
    var naslov = $("#naslov").val();
    var content = $("#text").val();
    var controler = $('#controler').val();

    $(".checkboxes :checked").each(function()
    {
      vrednost += $(this).val() + '||';
    });
    vrednost = vrednost.substring(0, (vrednost.length - 2));

    $.ajax({
      type: "POST",
      url: "/" + controler + "_dev.php/poruka/send",
      data: "naslov=" + naslov + "&content=" + content + "&profil_ids=" + vrednost,
      success: function(msg){
        if(msg)
        {
            $('#indicator').hide();
            $('#complete').html(msg);
        }
      }
    });
}

function send_odgovor(poruka_id)
{
    $("#dugme").addClass("hide");
    var vrednost = '';
    var content = $("#text").val();
    var controler = $('#controler').val();

    vrednost = vrednost.substring(0, (vrednost.length - 2));

    $.ajax({
      type: "POST",
      url: "/" + controler + "_dev.php/poruka/sendOdgovor",
      data: "content=" + content + "&poruka_id=" + poruka_id,
      success: function(msg){
        if(msg)
        {
            $('#indicator').hide();
            $('#complete').html(msg);
        }
      }
    });
}
